package ips.attack_log.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import cm.util.hibernate.HibernateSessionFactory;

public class AttackLogChart {
	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
    private Session session_1;
    private Transaction trans_1;
    private String hql_1;
    private Query query_1;
    private List l_1;
    private Session session_2;
    private Transaction trans_2;
    private String hql_2;
    private Query query_2;
    private List l_2;
    private Session session_3;
    private Transaction trans_3;
    private String hql_3;
    private Query query_3;
    private List l_3;
	public Session getSession() {
		return session;
	}


	public void setSession(Session session) {
		this.session = session;
	}


	public Transaction getTrans() {
		return trans;
	}


	public void setTrans(Transaction trans) {
		this.trans = trans;
	}


	public String getHql() {
		return hql;
	}


	public void setHql(String hql) {
		this.hql = hql;
	}


	public Query getQuery() {
		return query;
	}


	public void setQuery(Query query) {
		this.query = query;
	}


	public List getL() {
		return l;
	}


	public void setL(List l) {
		this.l = l;
	}


	public Session getSession_1() {
		return session_1;
	}


	public void setSession_1(Session session_1) {
		this.session_1 = session_1;
	}


	public Transaction getTrans_1() {
		return trans_1;
	}


	public void setTrans_1(Transaction trans_1) {
		this.trans_1 = trans_1;
	}


	public String getHql_1() {
		return hql_1;
	}


	public void setHql_1(String hql_1) {
		this.hql_1 = hql_1;
	}


	public Query getQuery_1() {
		return query_1;
	}


	public void setQuery_1(Query query_1) {
		this.query_1 = query_1;
	}


	public List getL_1() {
		return l_1;
	}


	public void setL_1(List l_1) {
		this.l_1 = l_1;
	}


	public Session getSession_2() {
		return session_2;
	}


	public void setSession_2(Session session_2) {
		this.session_2 = session_2;
	}


	public Transaction getTrans_2() {
		return trans_2;
	}


	public void setTrans_2(Transaction trans_2) {
		this.trans_2 = trans_2;
	}


	public String getHql_2() {
		return hql_2;
	}


	public void setHql_2(String hql_2) {
		this.hql_2 = hql_2;
	}


	public Query getQuery_2() {
		return query_2;
	}


	public void setQuery_2(Query query_2) {
		this.query_2 = query_2;
	}


	public List getL_2() {
		return l_2;
	}


	public void setL_2(List l_2) {
		this.l_2 = l_2;
	}


	public Session getSession_3() {
		return session_3;
	}


	public void setSession_3(Session session_3) {
		this.session_3 = session_3;
	}


	public Transaction getTrans_3() {
		return trans_3;
	}


	public void setTrans_3(Transaction trans_3) {
		this.trans_3 = trans_3;
	}


	public String getHql_3() {
		return hql_3;
	}


	public void setHql_3(String hql_3) {
		this.hql_3 = hql_3;
	}


	public Query getQuery_3() {
		return query_3;
	}


	public void setQuery_3(Query query_3) {
		this.query_3 = query_3;
	}


	public List getL_3() {
		return l_3;
	}


	public void setL_3(List l_3) {
		this.l_3 = l_3;
	}

	@SuppressWarnings("deprecation")
	public String execute() throws Exception
	{
		Date begindate = null;
		Date enddate = null;
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String curMonth = dateFormat.format(c.getTime());
		c.add(Calendar.MONTH, -1);
		String preMonth = dateFormat.format(c.getTime());
		begindate = dateFormat1.parse(preMonth);
		enddate = dateFormat2.parse(curMonth);
		//*******************************包异常检测告警*********************************************/		
				  session=HibernateSessionFactory.getSession();
				  trans=session.beginTransaction();
				  hql="select tm,count(*) from AbnormalDetect as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
				  query=session.createQuery(hql);
				  query.setString("t","abnormal_attack");//设置查询参数
				  l=query.list();
				  for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
			     {
					Object[] objects=(Object[])sit.next();
					Date date=(Date)objects[0];
					//介于起始时间和终止时间
		          if(((begindate.before(date))&&(enddate.after(date)))||(begindate.equals(date))||(enddate.equals(date)))
		          {
					SimpleDateFormat format = new SimpleDateFormat("MM月dd日"); 
					String s = format.format(date);
					
					long count=(Long)objects[1];
//					System.out.println("日期"+s);
//				    System.out.println("的病毒数量为"+count);
				    dataset.addValue(count, "数据包异常攻击次数", s);//设置条形图的值
		          }
		          }
//*******************************流量异常检测告警*********************************************/		
				  session_1=HibernateSessionFactory.getSession();
				  trans_1=session_1.beginTransaction();
				  hql_1="select tm,count(*) from AttackLog as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
				  query_1=session_1.createQuery(hql_1);
				  query_1.setString("t","flow_attack");//设置查询参数
				  l_1=query_1.list();
				  for(Iterator sit_1=l_1.iterator();sit_1.hasNext();)//迭代取值
			     {
					Object[] objects_1=(Object[])sit_1.next();
					Date date_1=(Date)objects_1[0];
					//介于起始时间和终止时间
		          if(((begindate.before(date_1))&&(enddate.after(date_1)))||(begindate.equals(date_1))||(enddate.equals(date_1)))
		          {
					SimpleDateFormat format_1 = new SimpleDateFormat("MM月dd日"); 
					String s_1 = format_1.format(date_1);
					
					long count_1=(Long)objects_1[1];
				    dataset.addValue(count_1, "流量异常攻击次数", s_1);//设置条形图的值
		          }
		          }
//*******************************滥用检测告警*********************************************/		
				  session_2=HibernateSessionFactory.getSession();
				  trans_2=session_2.beginTransaction();
				  hql_2="select tm,count(*) from AttackLog as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
				  query_2=session_2.createQuery(hql_2);
				  query_2.setString("t","abuse_attack");//设置查询参数
				  l_2=query_2.list();
				  for(Iterator sit_2=l_2.iterator();sit_2.hasNext();)//迭代取值
			     {
					Object[] objects_2=(Object[])sit_2.next();
					Date date_2=(Date)objects_2[0];
					//介于起始时间和终止时间
		          if(((begindate.before(date_2))&&(enddate.after(date_2)))||(begindate.equals(date_2))||(enddate.equals(date_2)))
		          {
					SimpleDateFormat format_2 = new SimpleDateFormat("MM月dd日"); 
					String s_2 = format_2.format(date_2);
					
					long count_2=(Long)objects_2[1];
				    dataset.addValue(count_2, "滥用攻击次数", s_2);//设置条形图的值
		          }
		          }
//*******************************DOS检测告警*********************************************/		
				  session_3=HibernateSessionFactory.getSession();
				  trans_3=session_3.beginTransaction();
				  hql_3="select tm,count(*) from dDos as al group by year(al.tm),month(al.tm),day(al.tm)";
				  query_3=session_3.createQuery(hql_3);
				  l_3=query_3.list();
				  for(Iterator sit_3=l_3.iterator();sit_3.hasNext();)//迭代取值
			     {
					Object[] objects_3=(Object[])sit_3.next();
					Date date_3=(Date)objects_3[0];
					//介于起始时间和终止时间
		          if(((begindate.before(date_3))&&(enddate.after(date_3)))||(begindate.equals(date_3))||(enddate.equals(date_3)))
		          {
					SimpleDateFormat format_3 = new SimpleDateFormat("MM月dd日"); 
					String s_3 = format_3.format(date_3);
					
					long count_3=(Long)objects_3[1];
				    dataset.addValue(count_3, "DOS/DDOS攻击次数", s_3);//设置条形图的值
		          }
		          }			  
////////////////////////////				  
				  SimpleDateFormat yue = new SimpleDateFormat( 
				  "yyyy年MM月dd日"); 
				  String y1 = yue.format(begindate); 
				  String y2 = yue.format(enddate);
				  JFreeChart chart = ChartFactory.createLineChart("最近一个月各个攻击告警走势图", // chart title
						    "日期", // domain axis label
						    "攻击次数", // range axis label
						    dataset, // data
						    PlotOrientation.VERTICAL, // orientation
						    true, // include legend
						    true, // tooltips
						    false // urls
						    );
				  chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
					//   NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...   
					    CategoryPlot plot = chart.getCategoryPlot();   
					    Font labelFont = new Font("宋体", Font.BOLD, 12);
					    plot.getDomainAxis().setLabelFont(labelFont); //x轴标题字体  
					    plot.getDomainAxis().setTickLabelFont(labelFont); //x轴刻度字体  
					    plot.getRangeAxis().setLabelFont(labelFont);  //y轴外围字体  
					    plot.getRangeAxis().setTickLabelFont(labelFont); //y轴标题字体  
					//   customise the range axis...   
					    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();   
					    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());   
					    rangeAxis.setAutoRangeIncludesZero(true); 
					    rangeAxis.setLabelFont(labelFont);
					    rangeAxis.setTickLabelFont(labelFont);
					    rangeAxis.setUpperMargin(0.20);   
					    rangeAxis.setLabelAngle(Math.PI / 2.0);   
					  
					    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();   
					//  renderer.setBaseShape(new Rectangle2D.Double(-1.5, -1.5, 3, 3), true);  //设置点   
					    renderer.setShape(new Rectangle2D.Double(-1.5, -1.5, 3, 3));   
					    renderer.setShapesVisible(true);   
					    renderer.setSeriesPaint(3,Color.red);   
					    renderer.setSeriesLinesVisible(3,false);   
					    renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示折点数据   	
					    chart.getLegend().setItemFont(labelFont);//设置最底下方框内的字体（图示）  
					    HttpServletResponse response = ServletActionContext.getResponse();
						try {
							ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
									950, 400);
							
						} catch (IOException e) {
							e.printStackTrace();
						}
						 System.out.println("成功");
						return null;
	}
}
